package com.example.minefieldkeepercloud.activity.machineslist;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.MachineListAdapter;
import com.example.minefieldkeepercloud.bean.BrandlAndModelBean;
import com.example.minefieldkeepercloud.bean.ByFactoryBean;
import com.example.minefieldkeepercloud.bean.CoinBean;
import com.example.minefieldkeepercloud.bean.FactoryNewBean;
import com.example.minefieldkeepercloud.bean.MacListBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.HttpUtils;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.pop.FactorySelectNewPop;
import com.example.minefieldkeepercloud.view.pop.MachineMaintainPop;
import com.example.minefieldkeepercloud.view.pop.StatePop;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机列表
 */
public class MachineListAc extends AppCompatActivity {
    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    @BindView(R.id.ly_top)
    LinearLayout ly_top;
    @BindView(R.id.tv_lx)
    TextView tv_lx;
    @BindView(R.id.tv_FullName)
    TextView tv_FullName;
    @BindView(R.id.tv_px)
    TextView tv_px;
    boolean bln_lx=false;
    private StatePop statePop;
    private FactorySelectNewPop factorySelectPop;
    private MachineMaintainPop machineMaintainPop;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    MachineListAdapter machineListAdapter;
    List<MacListBean.DataBean> listData=new ArrayList<>();
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private  MenuControl menuControl;
    @BindView(R.id.iv_1)
    ImageView iv_1;
    @BindView(R.id.iv_2)
    ImageView iv_2;
    @BindView(R.id.iv_3)
    ImageView iv_3;
    private String Sort="4", ZT="全部", factoryID="", IPS="", IPE="", BrandlID="",  ModelID="",
     BZID="3", WDmin="0", WDmax="999", SLmin="0", SLmax="99999", userID="";
    private int pageIndex=1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.machinelist);
        ButterKnife.bind(this);
        initView();
        getMineField();


    }

    private void initView()
    {
        machineListAdapter=new MachineListAdapter(MachineListAc.this,listData);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(machineListAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, R.color.bg);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);


        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        mRecyclerView.setLoadMoreEnabled(true);

        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {

                getListData();
            }
        });
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(MachineListAc.this, MachineDetailAc.class);
                Bundle bd=new Bundle();
                bd.putString("factoryID",factoryID);
                bd.putString("BZID",BZID);
                bd.putSerializable("data",listData.get(position));
                intent.putExtra("bd",bd);
                startActivity(intent);
                overridePendingTransition(0, 0);

            }
        });

        /**
         * 抽屉操作返回结果
         */
        menuControl=new MenuControl(MachineListAc.this, new MenuControl.MenuInterface() {
            @Override
            public void cancel() {
                drawerLayout.closeDrawer(Gravity.RIGHT);
            }

            @Override
            public void sure(String str_ip1, String str_ip2, String str_ip3, String str_ip4, String str_ip5, String str_brandId, List<String> list_modeId, String coinId, String Min_Sl, String Max_Sl, String Min_Wd, String Max_Wd, List<String> list_customerId) {
                drawerLayout.closeDrawer(Gravity.RIGHT);

                IPS=str_ip1+"."+str_ip2+"."+str_ip3+"."+str_ip4;
                IPE=str_ip1+"."+str_ip2+"."+str_ip3+"."+str_ip5;
                BrandlID=str_brandId;
                ModelID="";
                for (int i=0;i<list_modeId.size();i++)
                {
                    ModelID=ModelID+list_modeId.get(i)+",";
                }

                int m=ModelID.length()-1;
                if(m>0)
                ModelID=ModelID.substring(0,m);
                if(coinId.isEmpty())
                    BZID="3";
                else
                    BZID=coinId;
                if(Min_Wd.equals(""))
                    WDmin="0";
                else
                    WDmin=Min_Wd;
                if(Max_Wd.equals(""))
                    WDmax="999";
                else
                    WDmax=Max_Wd;
                if(Min_Sl.equals(""))
                    SLmin="0";
                else
                    SLmin=Min_Sl;
                if(Max_Sl.equals(""))
                    SLmax="99999";
                else
                    SLmax=Max_Sl;
                userID="";
                for (int i=0;i<list_customerId.size();i++)
                {
                    userID=userID+list_customerId.get(i)+",";
                }
                int n=userID.length()-1;
                if(n>0)
                userID=userID.substring(0,n);

                mRecyclerView.forceToRefresh();
            }


        });
    }

    /**
     * 获取矿场
     */
    private void getMineField() {
        if (!NetWorkUtil.check(this)) {
            return;
        }
        NetHelperNew.GetttGetFactory_New(new ApiHttpCallback(MachineListAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                FactoryNewBean factoryBean = MyGson.getInstance().fromJson(data, FactoryNewBean.class);
                if (factoryBean.getType() == 1) {
                    if (factoryBean.getData().size() > 0) {
                        CreatePop(factoryBean);
                    } else {
                        ToastUtil.showToast(MachineListAc.this, "没有可查询的矿场");
                    }

                } else {
                    finish();
                    ToastUtil.showToast(MachineListAc.this, factoryBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MachineListAc.this, "矿场查询请求失败");
                finish();
            }
        });


    }

    /**
     * 获取列表数据 dialog
     */
    private void getListData()
    {

        NetHelperNew.MachineMonitorScan_List(Sort, ZT, factoryID, IPS, IPE, BrandlID,  ModelID,
                BZID, WDmin, WDmax, SLmin, SLmax, userID,pageIndex+"",new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                Log.i("-->","矿机列表"+data);
                MacListBean macListBean = MyGson.getInstance().fromJson(data, MacListBean.class);
                if (macListBean.getType() == 1) {
                    if (macListBean.getData().size() > 0) {
                        pageIndex++;
                        listData.addAll(macListBean.getData());
                        machineListAdapter.setListData(listData);
                        machineListAdapter.notifyDataSetChanged();

                    } else {
                        mRecyclerView.setNoMore(true);
                        ToastUtil.showToast(MachineListAc.this, "没有可查询的数据");
                    }

                } else {

                    ToastUtil.showToast(MachineListAc.this, macListBean.getContent());
                }
                mRecyclerView.refreshComplete(20);

            }

                    @Override
                    public void onError(String msg) {
                        super.onError(msg);
                        ToastUtil.showToast(MachineListAc.this, msg);
                        mRecyclerView.refreshComplete(20);
                    }
                });
    }


    private void CreatePop(FactoryNewBean dataBean) {
        if(factoryID.isEmpty())
        {
            factoryID=dataBean.getData().get(0).getID()+ "";
        }
        if (factorySelectPop == null) {
            factorySelectPop = new FactorySelectNewPop(MachineListAc.this, dataBean, new FactorySelectNewPop.OnSelectListener() {
                @Override
                public void select(int id, String name) {
                    tv_FullName.setText(name);
                    factoryID=id+"";
                    getCustomer();
                    mRecyclerView.forceToRefresh();
                }
            });
        }


        tv_FullName.setText(dataBean.getData().get(0).getFactoryName());

        mRecyclerView.forceToRefresh();
        getCustomer();
        getBrand();
        getCoin();
    }

    /**
     * 获取品牌
     */
    private void getBrand()
    {

        NetHelperNew.GetBrandlAndModel(new ApiHttpCallback(MachineListAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                BrandlAndModelBean brandlAndModelBean = MyGson.getInstance().fromJson(data, BrandlAndModelBean.class);
                if (brandlAndModelBean.getType() == 1) {
                    if (brandlAndModelBean.getData().size() > 0) {

                        menuControl.setBrandData(brandlAndModelBean.getData());

                    } else {
                        ToastUtil.showToast(MachineListAc.this, "没有可查询的品牌");
                    }

                } else {
                    ToastUtil.showToast(MachineListAc.this, brandlAndModelBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MachineListAc.this, "品牌查询请求失败");
                finish();
            }
        });

    }

    /**
     * 获取币种
     */
    private void getCoin()
    {
        NetHelperNew.GetCurrency(new ApiHttpCallback(MachineListAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                CoinBean coinBean = MyGson.getInstance().fromJson(data, CoinBean.class);
                if (coinBean.getType() == 1) {
                    if (coinBean.getData().size() > 0) {

                        menuControl.setCoinData(coinBean.getData());

                    } else {
                        ToastUtil.showToast(MachineListAc.this, "没有可查询的币种");
                    }

                } else {
                    ToastUtil.showToast(MachineListAc.this, coinBean.getContent());
                }
            }
            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MachineListAc.this, "币种查询请求失败");
                finish();
            }
        });
    }

    /**
     * 获取客户
     */
    private void getCustomer()
    {

        NetHelperNew.GetTGByFactory(factoryID,new ApiHttpCallback(MachineListAc.this, 1,true) {
            @Override
            public void onSuccess(String data) {
                ByFactoryBean byFactoryBean = MyGson.getInstance().fromJson(data, ByFactoryBean.class);
                if (byFactoryBean.getType() == 1) {
                    if (byFactoryBean.getData().size() > 0) {

                        menuControl.setCustomer(byFactoryBean.getData());

                    } else {
                        ToastUtil.showToast(MachineListAc.this, "没有可查询的客户");
                    }

                } else {
                    ToastUtil.showToast(MachineListAc.this, byFactoryBean.getContent());
                }
            }
            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MachineListAc.this, "客户查询请求失败");
                finish();
            }
        });

    }

    @OnClick({R.id.title_left, R.id.title_center,R.id.ll_date,R.id.ly_lx,R.id.ly_kc,R.id.ly_px})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.title_center:
                finish();
                break;
            case R.id.ll_date://筛选
                drawerLayout.openDrawer(Gravity.RIGHT);
                break;
            case R.id.ly_lx://查看离线状态
//                     if(!bln_lx)
//                     {
////                         ZT="离线";
//                       //bln_lx=true;
////                         tv_lx.setTextColor(Color.parseColor("#00b9a7"));
//                         //
//                     }
//                     else
//                     {
////                         ZT="";
//                         //bln_lx=false;
//                        // tv_lx.setTextColor(Color.parseColor("#a5a5a5"));
//                         //iv_1.animate().rotation(0);
//                     }
                if(statePop==null)
                    statePop=new StatePop(MachineListAc.this, new StatePop.OnSelectListener() {
                        @Override
                        public void selectContent(String str) {
                            ZT=str;
                            tv_lx.setText(str);
                            mRecyclerView.forceToRefresh();
                        }
                    });
                statePop.showAsDropDown(ly_top, 0, 0);

                break;
            case R.id.ly_kc://矿场选择
                if (factorySelectPop != null)
                    factorySelectPop.showAsDropDown(ly_top, 0, 0);
                else
                    ToastUtil.showToast(MachineListAc.this, "没有可查询的矿场");
                break;
            case R.id.ly_px://排序
                if (machineMaintainPop == null)
                    machineMaintainPop=new MachineMaintainPop(MachineListAc.this, new MachineMaintainPop.OnSelectListener() {
                        @Override
                        public void selectContent(String str) {
                            tv_px.setText(str);
                        }

                        @Override
                        public void selectnumber(String str) {
                            Sort=str;
                            mRecyclerView.forceToRefresh();
                        }
                    });
                machineMaintainPop.showAsDropDown(ly_top, 0, 0);

                break;
        }
    }

    /**
     * 刷新
     */
    private void refresh()
    {
        pageIndex=1;
        listData.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        getListData();
    }


}
